//
//  JFBSettlementDS.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/16.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBSettlementDS.h"

@implementation JFBSettlementDS
- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.ID = [[attributes objectForKey: @"id"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"id"]  integerValue];
    self.start_at = [[attributes objectForKey: @"start_at"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"start_at"];
    self.end_at = [[attributes objectForKey: @"end_at"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"end_at"];
    self.total_settle_price = [[attributes objectForKey: @"total_settle_price"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"total_settle_price"];
    self.total_transfer_bonus = [[attributes objectForKey: @"total_transfer_bonus"] isEqual: [NSNull null]] ? 0 : [NSString formatDecimal:[attributes objectForKey:@"total_transfer_bonus"]];
    self.total_so_bonus = [[attributes objectForKey: @"total_so_bonus"] isEqual: [NSNull null]] ? 0 : [NSString formatDecimal:[attributes objectForKey:@"total_so_bonus"]];
    self.total_so_fine = [[attributes objectForKey: @"total_so_fine"] isEqual: [NSNull null]] ? 0 : [NSString formatDecimal:[attributes objectForKey:@"total_so_fine"]];
    self.shop_bonus = [[attributes objectForKey: @"shop_bonus"] isEqual: [NSNull null]] ? 0 : [NSString formatDecimal:[attributes objectForKey:@"shop_bonus"]];
    self.shop_fine = [[attributes objectForKey:@"shop_bonus"]isEqual: [NSNull null]] ? nil :[NSString formatDecimal:[attributes objectForKey:@"shop_bonus"]];
    return self;
}


+ (NSURLSessionDataTask *)loadSettlementList:(void (^)(NSArray *settlementList, NSError *error))block pager:(JFBPager*)pager{
    
    return [[JFBAPIClient sharedClient:true] GET:@"/shop/balance/list.json" parameters:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        if( error == [NSNull null] ) {
            NSLog(@"day payment:%@", JSON);
            NSMutableArray *result = [@[] mutableCopy];
            NSArray *response = [JSON valueForKeyPath:@"data"];
            
            for( NSDictionary *item in response ) {
                [result addObject:[[JFBSettlementDS alloc] initWithAttributes:item]];
            }
            block([NSArray arrayWithArray:result], nil);
        } else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
    return nil;
}
@end
